/* bitbang_ft2232.c

   Output some flickering in bitbang mode to the FT2232

   Thanks to max@koeln.ccc.de for fixing and extending
   the example for the second channel.

   This program is distributed under the GPL, version 2
*/

#include <stdio.h>
#include <unistd.h>
#include <octopus.h>

int main(int argc, char **argv)
{
#if 0
    struct octopus_context octopusc, octopusc2;
    char buf[1];
    int f,i;

    // Init 1. channel
    octopus_init(&octopusc);
    octopus_set_interface(&octopusc, INTERFACE_A);
    f = octopus_usb_open(&octopusc, 0x0403, 0x6001);
    if(f < 0 && f != -5) {
        fprintf(stderr, "unable to open octopus device: %d (%s)\n", f, octopus_get_error_string(&octopusc));
        exit(-1);
    }
    printf("octopus open succeeded(channel 1): %d\n",f);

    printf("enabling bitbang mode(channel 1)\n");
    octopus_enable_bitbang(&octopusc, 0xFF);

    // Init 2. channel
    octopus_init(&octopusc2);
    octopus_set_interface(&octopusc2, INTERFACE_B);
    f = octopus_usb_open(&octopusc2, 0x0403, 0x6001);
    if(f < 0 && f != -5) {
        fprintf(stderr, "unable to open octopus device: %d (%s)\n", f, octopus_get_error_string(&octopusc2));
        exit(-1);
    }
    printf("octopus open succeeded(channel 2): %d\n",f);

    printf("enabling bitbang mode (channel 2)\n");
    octopus_enable_bitbang(&octopusc2, 0xFF);

    // Write data
    printf("startloop\n");
    for(i = 0; i < 23; i++) {
        buf[0] =  0x1;
        printf("porta: %02i: 0x%02x \n",i,buf[0]);
        f = octopus_write_data(&octopusc, buf, 1);
        if(f < 0)
            fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, octopus_get_error_string(&octopusc));
        sleep(1);

        buf[0] =  0x2;
        printf("porta: %02i: 0x%02x \n",i,buf[0]);
        f = octopus_write_data(&octopusc, buf, 1);
        if(f < 0)
            fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, octopus_get_error_string(&octopusc));
        sleep(1);

        buf[0] =  0x1;
        printf("portb: %02i: 0x%02x \n",i,buf[0]);
        f = octopus_write_data(&octopusc2, buf, 1);
        if(f < 0)
            fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, octopus_get_error_string(&octopusc2));
        sleep(1);

        buf[0] =  0x2;
        printf("portb: %02i: 0x%02x \n",i,buf[0]);
        f = octopus_write_data(&octopusc2, buf, 1);
        if(f < 0)
            fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, octopus_get_error_string(&octopusc2));
        sleep(1);
    }
    printf("\n");

    printf("disabling bitbang mode(channel 1)\n");
    octopus_disable_bitbang(&octopusc);
    octopus_usb_close(&octopusc);
    octopus_deinit(&octopusc);

    printf("disabling bitbang mode(channel 2)\n");
    octopus_disable_bitbang(&octopusc2);
    octopus_usb_close(&octopusc2);
    octopus_deinit(&octopusc2);
  #endif
}
